/*
 * @lc app=leetcode.cn id=345 lang=java
 *
 * [345] 反转字符串中的元音字母
 *
 * https://leetcode-cn.com/problems/reverse-vowels-of-a-string/description/
 *
 * algorithms
 * Easy (48.40%)
 * Likes:    65
 * Dislikes: 0
 * Total Accepted:    20.5K
 * Total Submissions: 42.4K
 * Testcase Example:  '"hello"'
 *
 * 编写一个函数，以字符串作为输入，反转该字符串中的元音字母。
 *
 * 示例 1:
 *
 * 输入: "hello"
 * 输出: "holle"
 *
 *
 * 示例 2:
 *
 * 输入: "leetcode"
 * 输出: "leotcede"
 *
 * 说明:
 * 元音字母不包含字母"y"。
 *
 */

// @lc code=start
class Solution {
    public String reverseVowels(String s) {
        char[] ch = s.toCharArray();
        int left = 0, right = s.length()-1;
        while(left < right) {
            while(!isAlpha(ch[left]) && left < right) {
                //System.out.println(1);
                left++;
            }

            while(!isAlpha(ch[right]) && right > left) {
                right--;
            }

            char c = ch[left];
            ch[left] = ch[right];
            ch[right] = c;
            left++;right--;
        }
        return new String(ch);
    }

    boolean isAlpha(char ch) {
        return ch == 'a' || ch == 'e' || ch == 'i'
        || ch == 'u' || ch == 'o' || ch == 'A' || ch == 'E' || ch == 'I'
        || ch == 'U' || ch == 'O';
    }
}
// @lc code=end

